const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    prompt: '',
    default_img: {},
    order_goods_list: [],
    img_list: [],
    order_id: '',
    order_no: '',
    goods_evaluate: [], //评价信息
    scores: 5, //评价星级
    explain_type: 1, //1好评 2中评 3差评
    is_anonymous: 0, //是否匿名
    commentvFlag: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let again = options.again;
    let order_id = options.order_id;

    this.setData({
      again: again,
      order_id: order_id
    })
    app.defaultImg(this, true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    let again = that.data.again;
    if (again == 1) {
      wx.setNavigationBarTitle({
        title: '我要追评',
      })
    }
    app.restStatus(that, 'commentvFlag');
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 加载基础信息
   */
  loadInfo: function() {
    let that = this;
    let again = that.data.again;
    let order_id = that.data.order_id;
    let img_list = that.data.img_list;
    let goods_evaluate = that.data.goods_evaluate;
    let url = again == 0 ? 'System.Order.evaluationDetail' : 'System.Order.reviewEvaluateDetail';

    app.sendRequest({
      url: url,
      data: {
        order_id: order_id
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          let order_goods_list = data.list;
          let order_no = data.order_no;

          for (let index in order_goods_list) {
            //图片处理
            if (order_goods_list[index].picture_info != undefined) {
              let img = order_goods_list[index].picture_info.pic_cover_micro;
              order_goods_list[index].picture_info.pic_cover_micro = app.IMG(img);
            } else {
              order_goods_list[index].picture_info = {};
              order_goods_list[index].picture_info.pic_cover_micro = '';
            }
            goods_evaluate[index] = {};
            goods_evaluate[index].order_goods_id = order_goods_list[index].order_goods_id;
            goods_evaluate[index].imgs = '';
            goods_evaluate[index].img_num = 0;
            goods_evaluate[index].content = '';
            img_list[index] = [];
          }

          that.setData({
            order_id: order_id,
            order_no: order_no,
            order_goods_list: order_goods_list,
            goods_evaluate: goods_evaluate,
            img_list: img_list,
          })
        }
        console.log(res)
      }
    });
  },

  /**
   * 图片加载失败
   */
  errorImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let order_goods_list = that.data.order_goods_list;
    let default_img = that.data.default_img;
    let base = that.data.Base;
    let parm = {};
    let img = order_goods_list[index].picture_info.pic_cover_micro;

    if (default_img.is_use == 1) {
      default_img = default_img.value.default_goods_img;
      if (img.indexOf(default_img) == -1) {
        let parm_key = "order_goods_list[" + index + "].picture_info.pic_cover_micro";

        parm[parm_key] = default_img;
        that.setData(parm);
      }
    }
  },

  /**
   * 输入评价
   */
  inputContent: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let content = e.detail.value;
    let goods_evaluate = that.data.goods_evaluate;

    goods_evaluate[index].content = content;

    that.setData({
      goods_evaluate: goods_evaluate
    })
  },

  /**
   * 上传图片至微信服务器
   */
  uploadImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let goods_evaluate = that.data.goods_evaluate;

    if (goods_evaluate[index].img_num > 5) {
      return false;
    }
    //选择图片
    wx.chooseImage({
      count: 1,
      sizeType: 'compressed',
      success: function(res) {
        let file_Path = res.tempFilePaths[0];
        let tempFiles = res.tempFiles[0];

        //上传至服务器
        that.uplodeHeadImg(that, file_Path, index, goods_evaluate);
      },
      fail: function(res) {
        app.showBox(that, '无法获取本地图片');
        console.log(res);
      }
    })
  },

  /**
   * 上传至服务器
   */
  uplodeHeadImg: function (that, file_Path, index, goods_evaluate) {
    let img_list = that.data.img_list;
    let base = app.globalData.siteBaseUrl;

    if (file_Path == '') {
      wx.navigateBack({
        delta: 1
      })
    }

    let param = {
      token: app.globalData.token,
      file_path: 'comment',
      method: 'System.Upload.uploadImage'
    };
    param.sign = app.encodeSign(param);
    param.private_key = app.globalData.private_key;
    
    wx.uploadFile({
      url: base + 'api.php?method=System.Upload.uploadImage',
      filePath: file_Path,
      name: 'img_upload',
      formData: param,
      success: function(res) {

        let data = res.data;

        try {
          data = JSON.parse(JSON.parse(data));
        } catch (e) {
          console.log(e);
          app.showBox(that, '上传失败');
        }

        let code = data.code;
        if (code == 0) {
          data = data.data;
          let code = data.code;
          let message = data.message;
          let img_url = data.data;
          img_url = app.IMG(img_url);

          if (code > 0) {
            //加入图片数组
            if (goods_evaluate[index].imgs == '') {
              goods_evaluate[index].imgs = img_url;
              goods_evaluate[index].img_num = 1;
              img_list[index][0] = img_url;
            } else {
              goods_evaluate[index].imgs += ',' + img_url;
              goods_evaluate[index].img_num++;
              img_list[index][img_list[index].length] = img_url;
            }
            that.setData({
              goods_evaluate: goods_evaluate,
              img_list: img_list
            })
            console.log(img_list);
          } else {
            app.showBox(that, message);
          }

        } else {
          app.showBox(that, '上传失败');
        }
      },
      fail: function(res) {
        console.log(res);
        app.showBox(that, '上传失败');
      }
    })
  },

  /**
   * 删除图片
   */
  deleteImg: function(e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let key = e.currentTarget.dataset.key;
    let img_list = that.data.img_list;
    let goods_evaluate = that.data.goods_evaluate;

    img_list[index].splice(key, 1);
    goods_evaluate[index].imgs = img_list[index].join();
    goods_evaluate[index].img_num--;

    that.setData({
      img_list: img_list,
      goods_evaluate: goods_evaluate
    })
  },

  /**
   * 评价星级
   */
  stars: function(e) {
    let that = this;
    let scores = e.currentTarget.dataset.scores;
    let explain_type = 1;

    explain_type = parseInt(scores) > 1 ? 2 : 3;
    explain_type = parseInt(scores) > 3 ? 1 : explain_type;

    that.setData({
      scores: scores,
      explain_type: explain_type
    })
  },

  /**
   * 选择匿名
   */
  checkAnonymous: function(e) {
    let that = this;
    let is_anonymous = that.data.is_anonymous;

    is_anonymous = is_anonymous == 0 ? 1 : 0;

    that.setData({
      is_anonymous: is_anonymous
    })
  },

  /**
   * 发表评价
   */
  commentv: function() {
    let that = this;
    let again = that.data.again;
    let scores = that.data.scores;
    let order_id = that.data.order_id;
    let order_no = that.data.order_no;
    let is_anonymous = that.data.is_anonymous;
    let explain_type = that.data.explain_type;
    let goods_evaluate = that.data.goods_evaluate;
    let commentvFlag = that.data.commentvFlag;
    let url = again == 0 ? 'System.Order.addGoodsEvaluate' : "System.Order.addGoodsReviewEvaluate";

    if (commentvFlag == 1) {
      return false;
    }
    app.clicked(that, 'commentvFlag');

    for (let index in goods_evaluate) {
      goods_evaluate[index].scores = scores;
      goods_evaluate[index].explain_type = explain_type;
      goods_evaluate[index].is_anonymous = is_anonymous;
      if (goods_evaluate[index].content == '' || goods_evaluate[index].content == undefined) {
        app.showBox(that, '请输入要评价的内容');
        app.restStatus(that, 'commentvFlag');
        return false;
      }
    }

    app.sendRequest({
      url: url,
      data: {
        order_id: order_id,
        order_no: order_no,
        goods_evaluate: JSON.stringify(goods_evaluate)
      },
      success: function(res) {
        let code = res.code;
        let data = res.data;

        if (code == 0) {
          if (data > 0) {
            app.showBox(that, '评价成功');
            wx.navigateBack({
              delta: 1
            })
          } else {
            app.showBox(that, '评价失败');
            app.restStatus(that, 'commentvFlag');
          }
        }
        console.log(res)
      }
    });
  }
})